Test-Driven Machine Learning by Justin Bozonier

Test-Driven Machine Learning by Justin Bozonier

Author:Justin Bozonier [Bozonier, Justin]
Language: eng
Format: azw3
Publisher: Packt Publishing
Published: 2015-11-27T05:00:00+00:00


This test fails! Now, think about how you plan to get the test to pass. Since we need to handle multiple observations, let's refactor to store our, so far, single values in arrays so that we can easily generalize to what our test will need. After the refactoring, you will be left with a class that resembles the following:

class Classifier: def __init__(self): self._classifications = {} def train(self, classification, observation): if not classification in self._classifications: self._classifications[classification] = [] self._classifications[classification].append(observation) def classify(self, observation): if len(self._classifications.keys()) == 0: return None else: closest_class = self._classifications.keys()[0] closest_observation = abs(observation - self._classifications[closest_class][0]) for the_class, trained_observations in self._classifications.items(): if abs(observation - trained_observations[0]) < closest_observation: closest_class = the_class closest_observation = abs(observation - self._classifications[closest_class][0]) return closest_class



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.